package dyy.zc.note.ui.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.bmob.v3.datatype.BmobFile;
import dyy.zc.note.R;
import dyy.zc.note.application.MyApplication;
import dyy.zc.note.base.BaseActivity;
import dyy.zc.note.bean.NoteInfo;
import dyy.zc.note.bean.UserInfo;
import dyy.zc.note.constant.Constants;
import dyy.zc.note.utils.BmobUtils;
import dyy.zc.note.utils.DBControl;
import dyy.zc.note.utils.ToastUtils;
import dyy.zc.note.views.CleanableEditText;


public class LoginActivity extends BaseActivity implements View.OnClickListener {
    public UserInfo account = new UserInfo();
    private CleanableEditText phone,password;
    private Button login;
    private TextView register,forgetPassword;
    private String userPhone,userPassword;
    private BmobUtils bmobUtils;
    public Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case  Constants.ResultCode.RESULT_SUCCESS:
                    if (BmobUtils.loginSuccess){
                        dialog.dismiss();
                        // Log.e("id", account.getId() + "");
//                    account.setNikeName("小明");
//                    account.setRealName("小明");
                        //bmobUtils.updateAccountInfo(account);
                        //MyApplication.getInstance().saveUserInfo(account);
//                    Intent intent = new Intent();
//                    intent.putExtra("user",loginuser);
//                    startActivityForResult(intent,234);
//                    finish();
                        loginuser = MyApplication.getInstance().readLoginUser();
                        if (!loginuser.getNewInfo().equals("1")){
                            List<NoteInfo> list = new ArrayList<>();
                            DBControl dbControl = new DBControl(LoginActivity.this);
                            dbControl.query(list);
                            Collections.reverse(list);
                            if (list.size()!=0){
                                dialog = ProgressDialog.show(LoginActivity.this,null,"正在同步笔记......");
                                bmobUtils.syncNote(loginuser,list,7895,handler);
                            }else {
                                handler.sendEmptyMessage(23);
                            }
                        }else {
                            handler.sendEmptyMessage(895);
                        }


                    }else{
                        dialog.dismiss();
                        Toast.makeText(LoginActivity.this, "用户名或者密码错误", Toast.LENGTH_LONG).show();
                        password.setText("");
                    }
                    break;
                case 7895:
                    dialog.dismiss();
                    ToastUtils.shortToast(LoginActivity.this,"同步完成");
                    Intent intent = new Intent(LoginActivity.this,ChangeInfoActivity.class);
                    intent.putExtra("login",loginuser);
                    startActivity(intent);
//                    startActivityForResult(intent,444);
                    break;
                case 895:
                    loginuser = MyApplication.getInstance().readLoginUser();
                    Intent intent1 = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("save", loginuser);
                    intent1.putExtras(bundle);
                    setResult(456, intent1);
                    finish();
                    break;
                case 23:
                    Intent intent2 = new Intent(LoginActivity.this,ChangeInfoActivity.class);
                    intent2.putExtra("login",loginuser);
//                    startActivityForResult(intent2,444);
                    startActivity(intent2);
                    finish();
                    break;
            }
        }
    };
    @Override
    protected void findViewByIDS() {
        phone = findViewsById(R.id.phone);
        password = findViewsById(R.id.password);
        login = findViewsById(R.id.loginBtn);
        register = findViewsById(R.id.registertv);
        forgetPassword = findViewsById(R.id.forgetpwd);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loginuser = MyApplication.getInstance().readLoginUser();
        if (loginuser != null) {
            startActivity(ChangeInfoActivity.class);
            finish();
            return;
        }
        setContentView(R.layout.activity_login);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            getWindow().setFlags(
//                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        }
        bind();
    }

    private void bind() {
        login.setOnClickListener(this);
        register.setOnClickListener(this);
        bmobUtils = new BmobUtils(this);
        forgetPassword.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.loginBtn:
                checkAccountAndPassword();
                break;
            case R.id.registertv:
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                intent.putExtra("type","register");
                startActivity(intent);
                break;
            case R.id.forgetpwd:
                Intent intent1 = new Intent(LoginActivity.this,RegisterActivity.class);
                intent1.putExtra("type","find");
                startActivity(intent1);
                break;
        }
    }

    /**
     * 检查账号密码是否填写
     */
    private void checkAccountAndPassword() {
        userPhone = phone.getText().toString();
        userPassword = password.getText().toString();
        account.setAccount(userPhone);
        account.setPassword(userPassword);
        if (userPhone.equals("") || userPassword.equals("")) {
            Toast.makeText(LoginActivity.this, "请输入用户名或密码", Toast.LENGTH_LONG).show();
        } else {
            dialog = ProgressDialog.show(this, null, "正在登录...");
            bmobUtils.queryAccount(account);
        }
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        if (requestCode == 444){
//            if (data != null){
//                Intent intent1 = new Intent();
//                Bundle bundle = new Bundle();
//                bundle.putSerializable("save", loginuser);
//                intent1.putExtras(bundle);
//                setResult(456, intent1);
//                finish();
//            }
//        }
//    }
}
